using System;
using System.Diagnostics;
using System.IO;

namespace PSSymbolStore.Manager.Windows
{
    public class TroubleshootingInvoker
    {
        private const string Executable = "msdt.exe";
        private const string DiagnosticsPackageDirectory = "diagnostics";
        private const string DiagnosticsPackageName = "PSSymbolStore.diagpkg";
        private const string ArgsFormat = "/skip TRUE /path \"{0}\" /moreoptions FALSE ";
        private const string OptionalArgsFormat = "/param TS_RootSearchPath={0}";

        private static string GetDiagnosticsPath()
        {
            return Path.Combine(Path.Combine(Utilities.RunDirectory, DiagnosticsPackageDirectory), DiagnosticsPackageName);
        }

        public void Invoke(string inputPath)
        {
            string diagnosticsPath = GetDiagnosticsPath();
            if (!File.Exists(diagnosticsPath)) return;

            string args = String.Format(ArgsFormat, diagnosticsPath);
            
            // Crazy, but if the input path contains a space, msdt.exe
            // will barf. Stupid.
            if (!String.IsNullOrEmpty(inputPath) && !inputPath.Contains(" "))
            {
                args += String.Format(OptionalArgsFormat, inputPath);
            }

            Process.Start(Executable, args);
        }
    }
}